package com.base.common.properties;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.httpclient.NameValuePair;

import com.base.common.utils.ReadJarUtil;

/**
 * 获取下拉列表配置 2013-12-21 下午1:49:19
 * 
 * @author hubery.niejinzhen
 */
public class ComboConfig {
	private static Properties props = null;
	private static String propertresPath = "combox-config.properties";
	static {
		try {
			props = new Properties();
			InputStream inputStream = ReadJarUtil
					.getInputStream(propertresPath);
			props.load(inputStream);
			inputStream.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据配置关键字获取配置列表
	 * 
	 * @author hubery.niejinzhen
	 * @param key
	 * @return List<NameValuePair>
	 */
	public static List<NameValuePair> getConfig(String key) {
		String value = props.getProperty(key);
		if (value == null || "".equals(value)) {
			return null;
		}
		List<NameValuePair> configList = new ArrayList<NameValuePair>();
		// 开始解析配置信息
		if (value.startsWith("[") && value.endsWith("]")) {
			// 去掉前后中括号
			String newStr = value.substring(1, value.length() - 1);
			// 按逗号分割配置项
			String[] config = newStr.split(",");
			for (int i = 0; i < config.length; i++) {
				// 按分号分割name和value
				String[] nameValuePair = config[i].split(":");
				if (nameValuePair.length == 2) {
					// 前面为name，后面为value
					configList.add(new NameValuePair(nameValuePair[0],
							nameValuePair[1]));
				}
			}
		}
		return configList;
	}
}
